// beavtweet_testing.cpp: The Beav
// Description: a testing ground for the real beavtweet.cpp file

#include <iostream>
#include <fstream>
#include <twitcurl.h>
#include <curl/curl.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <vector>
using namespace std;

void beavtweetAuthorize()
{
twitCurl twitterObj;
string access_key = "";
string access_secret = "";
string keys_dir = getenv("HOME");
string key_path = keys_dir + "/.beavtweet_access_token_key.txt";
string secret_path = keys_dir + "/.beavtweet_access_token_secret.txt";
string authURL = "";
string authPIN;

string kConsumerKey = "bpVQ2b6C9PZwQ4GKCHdEw";
string kConsumerSecret = "t4VwN8ED7vMER08oUQDVuzsxqYyqSuuJILk0i4";

twitterObj.getOAuth().setConsumerKey(kConsumerKey);
twitterObj.getOAuth().setConsumerSecret(kConsumerSecret);

twitterObj.oAuthRequestToken(authURL);

cout << "\nYou must now authorize beavtweet to access your Twitter." << endl;
cout << "Beavtweet will open your browser to the URL below" << endl;
cout << "where you will login to Twitter & receive a PIN." << endl;
cout << "Then, you must return to beavtweet and paste PIN below." << endl;
cout << endl << authURL << endl << endl;
cout << "Hit ENTER to be re-directed to internet browser...";
cin.clear();
cin.ignore(10000, '\n');
getchar();
cout << endl << endl;

//try automatically opening URL
//ShellExecute(NULL, "open", authURL.c_str(), NULL, NULL, SW_SHOWNORMAL);

cout << "Enter PIN here: ";
cin >> authPIN;
cout << key_path << endl;
cout << secret_path << endl;

twitterObj.getOAuth().setOAuthPin(authPIN);
twitterObj.oAuthAccessToken();

twitterObj.getOAuth().getOAuthTokenKey(access_key);

twitterObj.getOAuth().getOAuthTokenSecret(access_secret);

ofstream token_key_store;
ofstream token_secret_store;

token_key_store.open(key_path.c_str());
token_secret_store.open(secret_path.c_str());

token_key_store.clear();
token_secret_store.clear();

token_key_store << access_key.c_str();

token_secret_store << access_secret.c_str();

token_key_store.close();
token_secret_store.close();
}


int beavtweetPostStatus()
{
twitCurl twitterObj;
string access_key = "";
string access_secret = "";
string keys_dir = getenv("HOME");
string key_path = keys_dir + "/.beavtweet_access_token_key.txt";
string secret_path = keys_dir + "/.beavtweet_access_token_secret.txt";
string web_response;
string curl_error;
string new_status;

ifstream get_token_key;
ifstream get_token_secret;

get_token_key.open(key_path.c_str());
get_token_secret.open(secret_path.c_str());

get_token_key >> access_key;
get_token_secret >> access_secret;

cout << access_key << endl;
cout << access_secret << endl;

get_token_key.close();
get_token_secret.close();

twitterObj.getOAuth().setOAuthTokenKey(access_key);
twitterObj.getOAuth().setOAuthTokenSecret(access_secret);


cout << endl << "Status: ";
getline(cin, new_status);

bool return_status = false;

if (twitterObj.statusUpdate(new_status))
  return_status = true;

if (std::string::npos != web_response.find("error"))
  return_status = false;

if (return_status == false)
  return 0;

else
  return 1;

}

/*++
* function: beavtweetTimeline
*
* description: returns users "home" timeline
*
* @input: none
*
* @output: returns http respone from users "home" timeline as a string
*
*--*/
string beavtweetTimeline()
{
twitCurl twitterObj;
string access_key = "";
string access_secret = "";
string keys_dir = getenv("HOME");
string key_path = keys_dir + "/.beavtweet_access_token_key.txt";
string secret_path = keys_dir + "/.beavtweet_access_token_secret.txt";
string web_response;

ifstream get_token_key;
ifstream get_token_secret;

get_token_key.open(key_path.c_str());
get_token_secret.open(secret_path.c_str());

get_token_key >> access_key;
get_token_secret >> access_secret;

get_token_key.close();
get_token_secret.close();

twitterObj.getOAuth().setOAuthTokenKey(access_key);
twitterObj.getOAuth().setOAuthTokenSecret(access_secret);

twitterObj.timelineFriendsGet();
twitterObj.getLastWebResponse(web_response);

return web_response;
}

/*++
* function: BTGetDirectMessages
*
* description: takes input string (such as html) and finds all text between two specified "tags" (eg. <text> & </text>).
* function will return each string it has found in a vector<string> with the first found being vector[0]
*
* @input: input_string=text to search, tag_one=tag which occurs 1st in set, tag_two=tag which occurs 2nd in set
*
* @output: returns vector<string> containing each set of text found
*
*--*/
string BTGetDirectMessages()
{
twitCurl twitterObj;
string access_key = "";
string access_secret = "";
string keys_dir = getenv("HOME");
string key_path = keys_dir + "/.beavtweet_access_token_key.txt";
string secret_path = keys_dir + "/.beavtweet_access_token_secret.txt";
string web_response;

ifstream get_token_key;
ifstream get_token_secret;

get_token_key.open(key_path.c_str());
get_token_secret.open(secret_path.c_str());

get_token_key >> access_key;
get_token_secret >> access_secret;

get_token_key.close();
get_token_secret.close();

twitterObj.getOAuth().setOAuthTokenKey(access_key);
twitterObj.getOAuth().setOAuthTokenSecret(access_secret);

twitterObj.directMessageGet();
twitterObj.getLastWebResponse(web_response);

return web_response;
}

/*++
* function: betweenTags
*
* description: takes input string (such as html) and finds all text between two specified "tags" (eg. <text> & </text>).
* function will return each string it has found in a vector<string> with the first found being vector[0]
*
* @input: input_string=text to search, tag_one=tag which occurs 1st in set, tag_two=tag which occurs 2nd in set
*
* @output: returns vector<string> containing each set of text found
*
*--*/
vector<string> betweenTags(string input_string, string tag_one, string tag_two)
{
  vector<string> results;
  int pos_one = 0; /*temporary variable for position of current tag_one*/
  int pos_two = 0; /*temporary variable for position of current tag_two*/
  
  /*if it can find both tags before reaching end of string*/
  if (input_string.find(tag_one) != string::npos && input_string.find(tag_two) != string::npos)
  {
    pos_one = input_string.find(tag_one);
    pos_two = input_string.find(tag_two);
    results.push_back(input_string.substr((pos_one+tag_one.size()),((pos_two-tag_two.size()+1)-pos_one)));
  }
  
  bool exit = 0;
  while (exit == 0)
  {
    pos_one = input_string.find(tag_one, pos_two);
    pos_two = input_string.find(tag_two, (pos_two+1));
    if (pos_one != string::npos && pos_two != string::npos)
      results.push_back(input_string.substr((pos_one+tag_one.size()),((pos_two-tag_two.size()+1)-pos_one)));
    else
      exit = 1;
  }
  
  return results;
}
